<style>
    .basic-info{background-color: #fff;border:1px solid #ddd;padding: 15px;}
    .form-group{margin-top: 10px;}
    .form-control{display:inline;width: 230px;border-radius: 4px;}
    /*.form-group>label{margin-left: 40px;font-weight: 400;}*/
    .signature,.qualification{background-color: #fff;margin-top: 20px;border:1px solid #ddd;padding: 15px;}
    #img_add_btn_wrap,#img_add_btn_wrap_cer{width: 100px;height: 100px;margin-left: 40px;margin-top: 10px; display:flex;justify-content: center;}
    #add_img_btn,#add_img_btn_cer{width:100px;font-size: 15px;text-align: center;line-height: 9.5;background: silver;}
    .img-list-wrap,.img-list-wrap-cer{display: flex;justify-content:flex-start;}
    .img-list-item,.img-list-item-cer{margin-top: 10px;margin-left: 20px;text-align: center;}
    .img_list_del_btn,.img_list_del_btn_cer{width:100px;font-size: 15px;color: red;text-align: center;}

</style>
<script type="text/javascript" src="/bootstrap_select/bootstrap-select.js"></script>
<link rel="stylesheet" type="text/css" href="/bootstrap_select/bootstrap-select.css">
<div class="nav-tabs-custom">
    <div class="tab-content">
        <ul class="nav nav-tabs">
            <li class="active">药品列表</li>
        </ul>
        <div class="tab-pane active" id="tab_2">
            <div class="box box-primary">
                <div class="box-header">
                    <form class="form-horizontal" method="get" action="{{ Request::url() }}" pjax-container>
                        <div class="form-group">

                            <div class="col-sm-2">
                                <input type="text" id="medicinal_name" name="medicinal_name" value="{{ Request::input('medicinal_name') }}"
                                       class="form-control" placeholder="请输入药品中文或英文名称">
                            </div>

                            <div class="col-sm-2">
                                <select name="insulin_type" id="insulinype" class="form-control">
                                    <option value="">请选择胰岛素类型</option>
                                    <option value="NONE" @if(Request::input('insulin_type') == 'NONE') selected="selected" @endif>NONE</option>
                                    <option value="SHORT_ACTING" @if(Request::input('insulin_type') == 'SHORT_ACTING') selected="selected" @endif>SHORT_ACTING</option>
                                    <option value="RAPID_ACTING" @if(Request::input('insulin_type') == 'RAPID_ACTING') selected="selected" @endif>RAPID_ACTING</option>
                                    <option value="INTERMEDIATE_ACTING" @if(Request::input('insulin_type') == 'INTERMEDIATE_ACTING') selected="selected" @endif>INTERMEDIATE_ACTING</option>
                                    <option value="BASAL" @if(Request::input('insulin_type') == 'BASAL') selected="selected" @endif>BASAL</option>
                                    <option value="PREMIXED" @if(Request::input('insulin_type') == 'PREMIXED') selected="selected" @endif>PREMIXED</option>
                                </select>
                            </div>
                            <div class="col-sm-2">
                                <select name="insulin_preparation" id="insulin_preparation" class="form-control">
                                    <option value="">请选择药品制剂</option>
                                    <option value="STANDARD" @if(Request::input('insulin_preparation') == 'STANDARD') selected="selected" @endif>STANDARD</option>
                                    <option value="U200" @if(Request::input('insulin_preparation') == 'U200') selected="selected" @endif>U200</option>
                                    <option value="U300" @if(Request::input('insulin_preparation') == 'U300') selected="selected" @endif>U300</option>
                                    <option value="U500" @if(Request::input('insulin_preparation') == 'U500') selected="selected" @endif>U500</option>
                                    <option value="UNKNOWN" @if(Request::input('insulin_preparation') == 'UNKNOWN') selected="selected" @endif>UNKNOWN</option>
                                </select>
                            </div>

                            <span class="col-sm-3">
                                <button type="submit" id="auth-button" class="btn btn-primary">查询</button>
                                <button type="button" class="btn btn-default" id="reset" style="margin-left: 20px;">重置</button>
                                <button type="button" id="btn_add"   class="btn btn-primary btn_bound" style="margin-left: 20px;">添加</button>
                            </span>
                        </div>
                    </form>
                </div>
            </div>
            <!--代理统计表格开始-->
            <div class="box">
                <div class="box-body">
                    <table class="table table-bordered table-hover text-center">
                        <thead>
                        <tr>
                            <th>药品英文名</th>
                            <th>药品中文名</th>
                            <th>图片</th>
                            <th>药品标签</th>
                            <th>药品类型</th>
                            <th>药品制剂</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        @foreach ($result as $item)
                            <tr>
                                <td>{{$item->medicinal_en}}</td>
                                <td>{{$item->medicinal_cn}}</td>
                                <td><img width="100" height="100" src="{{$item->image}}"> </td>

                                <td>
                                    @if($item->tag == '1')
                                        胰岛素
                                    @elseif($item->tag == '2')
                                        口服药
                                    @else
                                        胰岛素
                                    @endif
                                </td>
                                <td>{{$item->insulin_type}}</td>
                                <td>{{$item->insulin_preparation}}</td>

                                <td>
                                    <button type="button" data_id = "{{$item->id}}"  class="btn btn-success btn_save">编辑</button>
                                    <button type="button" class="btn btn-danger" onclick="delete_device({{$item->id}})">删除</button>
                                </td>
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                    {{ $result->appends(['brand_name' => Request::input("brand_name"),'status'=>Request::input("status")])->links() }}
                </div>
            </div>
        </div>
    </div>
</div>

<form action="/admin/Device/addFacory" method="post" accept-charset="UTF-8">
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content" style="border-radius: 10px;margin-top:30%;">
                <div class="modal-header" style="background: #3c8dbc;color: #fff;border-top-left-radius:10px;border-top-right-radius: 10px;">
                    <h4 class="modal-title" id="myModalLabel">新增</h4>
                </div>
                <input type="hidden" name="medicinal_id" class="form-control" id="medicinal_id">

                <div class="modal-body">

                    <div class="form-group" style="width: 60%">
                        <label for="txt_departmentname">药品标签：</label>
                        <select class="form-control" id="tag">
                            <option value="1">胰岛素
                            </option>
                            <option value="2">口服药
                            </option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="txt_departmentname">药品英文名：</label>
                        <input type="text" class="form-control" id="medicinal_en" placeholder="请输入">
                    </div>
                    <div class="form-group">
                        <label for="txt_departmentname">药品中文名：</label>
                        <input type="text" class="form-control" id="medicinal_cn" placeholder="请输入">
                    </div>

                    <div class="form-group" style="width: 60%">
                        <label for="txt_departmentname">药品类型：</label>
                        <select class="form-control" id="type" >
                            <option value="">请选择药品类型</option>
                            <option value="NONE">NONE</option>
                            <option value="SHORT_ACTING">SHORT_ACTING</option>
                            <option value="RAPID_ACTING">RAPID_ACTING</option>
                            <option value="INTERMEDIATE_ACTING">INTERMEDIATE_ACTING</option>
                            <option value="BASAL">BASAL</option>
                            <option value="PREMIXED">PREMIXED</option>
                        </select>
                    </div>

                    <div class="form-group" style="width: 60%">
                        <label for="txt_departmentname">药品制剂：</label>
                        <select id="preparation" class="form-control">
                            <option value="">请选择药品制剂</option>
                            <option value="STANDARD">STANDARD</option>
                            <option value="U200">U200</option>
                            <option value="U300">U300</option>
                            <option value="U500">U500</option>
                            <option value="UNKNOWN">UNKNOWN</option>
                        </select>
                    </div>

                    <div class="qualification">
                        <div style="font-size: 16px;font-weight: 900;">图片上传</div>
                        <div class="img-list-wrap-cer">
                            <div id="img_add_btn_wrap_cer"> <a id="add_img_btn_cer" href="javascript:void(0)"><i class="fa fa-plus-circle fa-5x"></i></a></div>

                        </div>

                    </div>

                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取 消</button>
                        <button type="submit" id="btn_submit" class="btn btn-primary" data-dismiss="modal"  style="margin-left: 20px;">确 定</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>
<form method="post" id="form3" >
    <input type="file" name="photo" id="add_img_cer" style="display: none;"/>
</form>

<script type="text/javascript">
    $('#reset').click(function () {
        $('#medicinal_name').val('');
        $('#insulinype').val('');
        $('#insulin_preparation').val('');
    });
    //资质文件
    $('#add_img_btn_cer').on('click',function () {
        var length = $('.img-list-item-cer').size();
        if ( length >= 1 ) {
            layer.msg('图片已存在');return;
        }
        $('#add_img_cer').click()
    });

    $('#add_img_cer').on('change',function (e) {
        if($('#add_img_cer').val()==''){
            return
        }
        var index = layer.load(0, {shade: '#ccc'}); //0代表加载的风格，支持0-2
        //loading层
        $('#form3').ajaxSubmit({type:'post',url:'/admin/shop/upload_loop_img',success:function (res) {
            var html = "<div class=\"img-list-item-cer\"> <img class='facory_img' width=\"100\" height=\"100\" src=\""+ res.data +"\"\/><br\/><a class=\"img_list_del_btn_cer\" data_img=\""+res.data+"\" href=\"javascript:void(0)\">删除<\/a><\/div>";
            $('.img-list-wrap-cer').append(html);
            layer.close(index);
        }})
    });

    $(document).on('click','.img_list_del_btn_cer',function (e) {
        var index = layer.load(0, {shade: '#ccc'});
        var value = $(this).attr('data_img');
        $(this).closest('.img-list-item-cer').remove();
        layer.close(index);
    });

    $("#btn_add").click(function () {
//        admin_toastr('Message...', 'warning');
        $("#myModalLabel").text("添加设备");
        $('#myModal').modal();
        $('#facory_id').val(0);
    });

    //修改
    $(".btn_save").click(function () {
        $("#myModalLabel").text("编辑设备");
        $('#myModal').modal();
        var id = $(this).attr('data_id');
        //Ajax获取详情
        $.ajax({
            url : 'one_medicinal',
            data:{'id':id},
            success:function(result){
                var data = result.data;
                $('#medicinal_en').val(data.medicinal_en);
                $('#medicinal_cn').val(data.medicinal_cn);
                $('#tag').val(data.tag);
                $('#type').val(data.insulin_type);
                $('#preparation').val(data.insulin_preparation);
                $('#medicinal_id').val(data.id);
                if(data.image){
                    var html = "<div class=\"img-list-item-cer\"> <img class='facory_img' width=\"100\" height=\"100\" src=\""+ data.image +"\"\/><br\/><a class=\"img_list_del_btn_cer\" data_img=\""+data.image+"\" href=\"javascript:void(0)\">删除<\/a><\/div>";
                    $('.img-list-wrap-cer').append(html);
                }
            }
        });
    });


    $('#btn_submit').on('click',function (e) {
        var data = {};
        data['medicinal_en'] = $('#medicinal_en').val();
        data['medicinal_cn'] = $('#medicinal_cn').val();
        data['tag'] = $('#tag').val();
        data['type'] = $('#type').val();
        data['preparation'] = $('#preparation').val();

        data['id'] = $('#medicinal_id').val();
        data['image'] = $('.facory_img').attr('src');

        if(data['medicinal_en'] == "" || data['medicinal_cn'] == "" || data['tag'] == "" || data['type'] == "" || data['preparation'] == "") {
            alert('缺少参数');return;
        }

        $.ajax({
            url :  'add_medicinal',
            type:'post',
            dataType:'json',
            data:data,
            success:function(result){
                if ( result.code != 0 ) {
                    layer.msg('操作失败')

                }else{

                    layer.msg('操作成功', {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        location.reload();
                    });

                }
            }
        });
    })

    function delete_device(id){

        $.ajax({
            url :  'delete_medicinal',
            type:'post',
            dataType:'json',
            data: {
                id: id,
            },
            success:function(result){
                console.log(result);
                if ( result.code != 0 ) {
                    layer.msg(result.msg)

                }else{

                    layer.msg('删除成功', {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        location.reload();
                    });

                }
            }
        });
    }


</script>
